﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services;
using FukiERP.Services.Helper;
using FukiERP.Services.DBContext;

namespace FukiERP.Product
{
    public partial class Form_Model : Telerik.WinControls.UI.RadForm
    {
        int _currentID = 0;

        public Form_Model()
        {
            InitializeComponent();
        }

        private void Form_Model_Load(object sender, EventArgs e) {
            // Load Model Brand List
            FukiERPDataContext context = DataAccess.GetDBContext();
            List<ModelBrand> mBrand = context.ModelBrands.OrderBy(c => c.Name).ToList();
            drp_ModelBrand.DataSource = mBrand;
            drp_ModelBrand.DisplayMember = ColumName.Col_Name;
            drp_ModelBrand.Text = CommonConstants.DefaultDropDownValue;
            LoadData();
        }

        private void LoadData() {
            grd_Model.Rows.Clear();
            FukiERPDataContext context = DataAccess.GetDBContext();
            List<View_BrandAndModel> model = context.View_BrandAndModels.OrderBy(c => c.ModelBrandName).
                ThenBy(c => c.Name).ToList();
            grd_Model.DataSource = model;
        }

        private void tbn_Save_Click(object sender, EventArgs e) {
            Model model = null;
            FukiERPDataContext context = DataAccess.GetDBContext();
            errorProvider1.Clear();
            bool isValid = ValidateValue.ValidateRequirement(errorProvider1, tbx_ModelName);
            isValid = isValid && ValidateValue.ValidateDropDown(errorProvider1, drp_ModelBrand);
            if (!isValid) {
                MessageBox.Show(MessageConstants.GaneralValidateMessage);
                return;
            }
            if (_currentID != 0) {
                model = context.Models.Where(c => c.ID == _currentID).FirstOrDefault();
            }
            if (model == null) {
                model = new Model();
                context.Models.InsertOnSubmit(model);
            }
            model.Name = tbx_ModelName.Text;
            model.Description = tbx_ModelDes.Text;
            model.ModelBrand = ((ModelBrand) drp_ModelBrand.SelectedValue).ID;
            context.SubmitChanges();
            RefreshForm();
            LoadData();
        }

        private void btn_Delete_Click(object sender, EventArgs e) {
            if (_currentID != 0 && PresentHelper.ShowConfirmBox(MessageConstants.ConfirmDeleteMessage)) {
                FukiERPDataContext context = DataAccess.GetDBContext();
                Model model = context.Models.Where(c => c.ID == _currentID).FirstOrDefault();
                if (model != null) {
                    context.Models.DeleteOnSubmit(model);
                    context.SubmitChanges();
                }
                MessageBox.Show(MessageConstants.DeleteSuccessMessage);
                RefreshForm();
                LoadData();
            }
        }

        private void btn_Create_Click(object sender, EventArgs e) {
            RefreshForm();
        }

        private void RefreshForm() {
            _currentID = 0;
            tbx_ModelName.Text = string.Empty;
            tbx_ModelDes.Text = string.Empty;
            drp_ModelBrand.Text = CommonConstants.DefaultDropDownValue;
        }

        private void grd_Model_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e) {
            _currentID = int.Parse(grd_Model.Rows[e.RowIndex].Cells[0].Value.ToString());
            View_BrandAndModel model = (View_BrandAndModel)grd_Model.Rows[e.RowIndex].DataBoundItem;
            tbx_ModelName.Text = model.Name;
            tbx_ModelDes.Text = model.Description;
            ModelBrand mBrand = ((List<ModelBrand>) drp_ModelBrand.DataSource).
                Where(c => c.ID == model.ModelBrand).FirstOrDefault();
            if (mBrand != null) {
                drp_ModelBrand.SelectedValue = mBrand;
                drp_ModelBrand.Text = mBrand.Name;
            }
        }
    }
}
